<!--  -->
<template>
  <div>
    <div class="pending">
      <!-- 搜索 -->
      <div class="formSearch">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="品牌名称">
            <el-input v-model="formInline.name" placeholder="请输入" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" plain @click="search">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      
      <el-button type="primary" @click="add" v-permission="['brand:add']">新增品牌</el-button>
      <!-- 表格 -->
      <div class="tableBox">
        <el-table
          ref="multipleTable"
          :data="tableData"
          border
          :header-cell-style="{ background: '#EEF3FF', color: '#333333' }"
          tooltip-effect="dark"
          style="width: 100%"
        >
          <el-table-column label="订单id" >
            <template slot-scope="scope">{{ scope.row.id }}</template>
          </el-table-column>
          <el-table-column prop="name" label="品牌名称" />
          <el-table-column prop="createId" label="创建人id" width="220" />
           <el-table-column label="品牌图片" align="center">
            <template slot-scope="scope">
                <img :src="scope.row.brandImage" style="width:100px;height:100px"  @click="previewPic(scope.row.brandImage)"/>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" />
          <el-table-column label="操作" show-overflow-tooltip align="center">
            <template slot-scope="scope">
              <div class="btnList">
                <el-button type="text" @click="modify(scope.row)" v-permission="['brand:modify']">修改</el-button>
                <el-button type="text" @click="seeDetail(scope.row)" v-permission="['brand:detail']">详情</el-button>
                <el-button type="text" @click="brandDelete(scope.row)" v-permission="['brand:detele']">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div class="fenye">
          <el-pagination
            :current-page="currentPage"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>

     <el-dialog
      title="新增品牌1"
      :visible.sync="addFormDialog"
      width="600px"
      :close-on-click-modal="false"
    >
      <!-- 新增-->
      <div>
        <el-form ref="addForm" :model="addForm" label-width="80px" :rules="userRules">
          <el-form-item label="品牌名称" prop="name">
            <el-input v-model="addForm.name" placeholder="请输入品牌名称" />
          </el-form-item>
          <el-form-item label="品牌图片" prop="brandImage">
            <el-upload
              :class="{ uoloadSty: showBtnImg, disUoloadSty: noneBtnImg }"
              list-type="picture-card"
              :on-preview="previewPic"
              :on-remove="handleRemove"
              :headers="headers"
              :file-list="addForm.brandImage"
              :limit="1"
              :on-success="handleAvatarSuccess"
              :action="fileUploadApi.fileUpload"
            >
              <i slot="default" class="el-icon-plus" />
              <div slot="file" slot-scope="{ file }">
                <img
                  class="el-upload-list__item-thumbnail"
                  :src="file.imgPath"
                />
                <span class="el-upload-list__item-actions">
                  <span
                    class="el-upload-list__item-preview"
                    @click="handlePictureCardPreview(file)"
                  >
                    <i class="el-icon-zoom-in" />
                  </span>

                  <span
                    class="el-upload-list__item-delete"
                    @click="handleRemove(file)"
                  >
                    <i class="el-icon-delete" />
                  </span>
                </span>
              </div>
            </el-upload>
          </el-form-item>
            </el-form-item>
             <el-form-item label="排序">
            <el-input v-model="addForm.sort" placeholder="请输入角色描述" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addFormDialog = false">取 消</el-button>
        <el-button type="primary" @click="addForm_enter('addForm')">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="修改品牌"
      :visible.sync="editFormDialog"
      width="600px"
      center
      :close-on-click-modal="false"
    >
      <!-- 修改-->
      <div>
        <el-form ref="editForm" :model="editForm" label-width="80px" :rules="editRules">
          <el-form-item label="品牌名称" prop="name">
            <el-input v-model="editForm.name" placeholder="请输入角色名称" />
          </el-form-item>
          <el-form-item label="品牌图片" prop="brandImage">
            <el-upload
              :class="{ uoloadSty: showBtnImg, disUoloadSty: noneBtnImg }"
              list-type="picture-card"
              :on-preview="previewPic"
              :on-remove="handleRemove1"
              :headers="headers"
              :file-list="editForm.brandImage"
              :limit="1"
              :on-success="handleAvatarSuccess1"
              :action="fileUploadApi.fileUpload"
            >
              <i slot="default" class="el-icon-plus" />
              <div slot="file" slot-scope="{ file }">
                <img
                  class="el-upload-list__item-thumbnail"
                  :src="file.imgPath"
                />
                <span class="el-upload-list__item-actions">
                  <span
                    class="el-upload-list__item-preview"
                    @click="handlePictureCardPreview(file)"
                  >
                    <i class="el-icon-zoom-in" />
                  </span>

                  <span
                    class="el-upload-list__item-delete"
                    @click="handleRemove1(file)"
                  >
                    <i class="el-icon-delete" />
                  </span>
                </span>
              </div>
            </el-upload>
          </el-form-item>
             <el-form-item label="排序">
            <el-input v-model="editForm.sort" placeholder="请输入角色描述" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editFormDialog = false">取 消</el-button>
        <el-button type="primary" @click="editForm_enter('editForm')">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="查看详情"
      :visible.sync="detailFormDialog"
      width="600px"
      center
      :close-on-click-modal="false"
    >
      <!-- 详情-->
      <div>
        <el-form ref="ruleForm" :model="addForm" label-width="80px" :rules="detailRules">
          <el-form-item label="品牌名称" prop="roleName">
            <el-input v-model="detailForm.name" placeholder="请输入角色名称" />
          </el-form-item>
          <el-form-item label="品牌图片">
             <el-upload
              :class="{ uoloadSty: showBtnImg, disUoloadSty: noneBtnImg }"
              list-type="picture-card"
              :on-preview="previewPic"
              :on-remove="handleRemove"
              :headers="headers"
              :file-list="detailForm.brandImage"
              :limit="1"
              :on-success="handleAvatarSuccess"
              :action="fileUploadApi.fileUpload"
            >
              <i slot="default" class="el-icon-plus" />
              <div slot="file" slot-scope="{ file }">
                <img
                  class="el-upload-list__item-thumbnail"
                  :src="file.imgPath"
                />
                <span class="el-upload-list__item-actions">
                  <span
                    class="el-upload-list__item-preview"
                    @click="handlePictureCardPreview(file)"
                  >
                    <i class="el-icon-zoom-in" />
                  </span>
                </span>
              </div>
            </el-upload>
          </el-form-item>
             <el-form-item label="排序">
            <el-input v-model="detailForm.sort" placeholder="请输入角色描述" />
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>

    <!-- 图片预览 -->
      <el-dialog :visible.sync="dialogVisible" title="图片预览" width="50%">
        <img :src="previewpic" alt=""  width="100%" />
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
import {
  brandgetAll,
  brandSave,
  brandUpdate,
  getDetail,
  brandDelete,
} from "@/api/brand";
import api from "@@/components/canvasShow/config/api";
export default {
  components: {},
  data() {
    // 这里存放数据
    return {
      userRules:{
        name: [
          { required: true, message: "请输入品牌名称", trigger: "blur" },
        ],
        brandImage: [
          { required: true, message: "请上传品牌图片", trigger: "blur" },
        ],
      },
      editRules:{
        name: [
          { required: true, message: "请输入品牌名称", trigger: "blur" },
        ],
        brandImage: [
          { required: true, message: "请上传品牌图片", trigger: "blur" },
        ],
      },
      detailRules:{
        name: [
          { required: true, message: "请输入品牌名称", trigger: "blur" },
        ],
        brandImage: [
          { required: true, message: "请上传品牌图片", trigger: "blur" },
        ],
      },
      headers: {
        Authorization: "",
      },
      showBtnImg: true,
      noneBtnImg: false,
      dialogVisible: false,
      previewpic: "",
      detailFormDialog: false, //详情
      tip: "建议尺寸: 1080*900px, 高度自适应",
      fileUploadApi: {
        fileUpload: "",
      },
      detailForm: {
        name: "",
        brandImage: "",
        sort: 0,
      },
      editFormDialog: false,
      editForm: {
        name: "",
        brandImage: "",
        sort: 0,
      },
      addForm: {
        name: "",
        brandImage: [],
        sort: 0,
      },
      addFormDialog: false,
      formInline: {
        name: "", // 处理状态 0-待处理 1-已处理
        page: 1,
        pageSize: 10,
      },
      total: 1,
      tableData: [],
      currentPage: 1,
    };
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.fileUploadApi = api;
    this.brandgetAll(this.formInline);
  },
  // 方法集合
  methods: {
    // 移除图片
    handleRemove(file) {
      this.showBtnImg = true;
      this.noneBtnImg = false;
      const { imgPath } = file;
      this.addForm.brandImage = this.addForm.brandImage.filter((item) => {
        return item.imgPath !== imgPath;
      });
    },

    handleRemove1(file) {
      this.showBtnImg = true;
      this.noneBtnImg = false;
      const { imgPath } = file;
      this.editForm.brandImage = this.addForm.brandImage.filter((item) => {
        return item.imgPath !== imgPath;
      });
    },

    //预览大图
    previewPic(url) {
      this.previewpic = url;
      this.dialogVisible = true;
    },

    brandDelete(row) {
      brandDelete({ id: row.id }).then((res) => {
        this.editFormDialog = false;
        this.$message({
          message: "删除成功",
          type: "success",
        });
        this.brandgetAll(this.formInline);
      });
    },
    modify(row) {
      this.editFormDialog = true;
      this.showBtnImg = false;
      this.noneBtnImg = true;
      this.editForm.name = row.name;
      this.editForm.sort = row.sort;
      this.editForm.id = row.id;
      //组合classifyImage数据结构
      let array = new Array();
      array.push({
        imgPath: row.brandImage,
      });
      this.editForm.brandImage = array;
    },
    editForm_enter(editform) {
      this.$refs[editform].validate((valid) => {
        if (valid) {
          let editForm = {
            name:this.editForm.name,
            sort:this.editForm.sort,
            id:this.editForm.id,
            brandImage:this.editForm.brandImage[0].imgPath
          }
          brandUpdate(editForm).then((res) => {
            this.editFormDialog = false;
            this.$message({
              message: "修改成功",
              type: "success",
            });
            this.brandgetAll(this.formInline);
          });
         } else {
          //console.log("error submit!!");
          return false;
        }
      });
    },
    //查看详情
    seeDetail(row) {
      this.showBtnImg = false;
      this.noneBtnImg = true;
      getDetail(row.id).then((res) => {
        this.detailFormDialog = true;
        this.detailForm = res.data;
        let array = new Array();
        array.push({
          imgPath: row.brandImage,
        });
        this.detailForm.brandImage = array;
      });
    },
    // 新增角色
    add() {
      this.userState = 1;
      this.addFormDialog = true;
      this.addForm = {
        name: "",
        brandImage:[],
        sort: 0,
      };
    },
    // 确认新增角色
    addForm_enter(addForm) {
      this.$refs[addForm].validate((valid) => {
        if (valid) {
          console.log('test查看进程')
          if (this.addForm.brandImage.length) {
            this.addForm.brandImage =
            this.addForm.brandImage[0].imgPath;
          }
          brandSave(this.addForm).then((res) => {
            //console.log(res);
            this.$message({
              message: "新增成功",
              type: "success",
            });
            this.brandgetAll(this.formInline);
            this.addFormDialog = false;
          });
        } else {
          //console.log("error submit!!");
          return false;
        }
      });
    },

    handleAvatarSuccess(response, file) {
      const { url } = response.data;
      this.addForm.brandImage.push({
        imgPath: url,
      });
      this.showBtnImg = false;
      this.noneBtnImg = true;
    },

    handleAvatarSuccess1(response, file) {
      const { url } = response.data;
      this.editForm.brandImage.push({
        imgPath: url,
      });
      this.showBtnImg = false;
      this.noneBtnImg = true;
    },
    beforeAvatarUpload(file) {
      const isLt1M = file.size / 1024 / 1024 < 1;
      if (!isLt1M) {
        this.$message.error("上传图片大小不能超过 1MB!");
      }
      return isLt1M;
    },
    showImage() {
      this.editForm.brandImage = this.imageUrl;
      this.dialogVisible = true;
    },
    delImage() {
      this.$emit("update:imageUrl", "");
    },

    handleSizeChange(val) {
      this.formInline.pageSize = val;
      this.getAll(this.formInline);
    },
    handleCurrentChange(val) {
      this.formInline.page = val;
      this.getAll(this.formInline);
    },
    handleClick(tab, event) {
      //console.log(tab, event);
      this.formInline.state = tab.name;
      this.getAll(this.formInline);
    },
    //  查询
    search() {
      this.formInline.page = 1;
      this.brandgetAll(this.formInline);
    },
    // 查看
    seeMore(row, index) {
      row.type = index;
      this.$router.push({
        name: "afterDetails",
        query: { info: row },
      });
    },
    // 编辑
    edit() {},
    // 删除
    del() {},
    // 初始化查询所有数据
    async brandgetAll(formInline) {
      const res = await brandgetAll(formInline);
      this.tableData = res.data.list;
      this.total = parseInt(res.data.total);
    },
  },
};
</script>
<style lang='scss' scoped>
//@import url(); 引入公共css类
::v-deep .el-upload--picture-card {
  width: 148px;
  height: 148px;
  line-height: 148px;
}
::v-deep .disUoloadSty .el-upload--picture-card {
  display: none !important; /* 上传按钮隐藏 */
}
.pending {
  padding: 30px;
}
.fenye {
  margin-top: 20px;
}
</style>
